﻿using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Xml;
using System.Data;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.System;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Collections.Generic;

/// <summary>
/// Summary description for MessageService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class MessageService : System.Web.Services.WebService {

    
    #region Property
    public Authentication SessionHeader
    {
        get;
        set;
    }
    #endregion

    [WebMethod]
    [SoapHeader("SessionHeader", Direction = SoapHeaderDirection.In)]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetMessageList(String UserID) {
        //验证请求是否有效
        DataResult result = new DataResult();
        if (ValidRequest(new String[] { UserID, SessionHeader.RequestKey }, SessionHeader.Version, SessionHeader.UniqueKey))
        {
            result.Message = "验证失败";
        }
        else
        {
            try
            {
                //修改为自动获取当前的虚拟目录路径
                string path = null;
                try
                {
                    path = Context.Request.Url.ToString().Substring(0, Context.Request.Url.ToString().ToLower().LastIndexOf("/") + 1);
                }
                catch { }
                List<Message> list = new List<Message>();
                Jumpton.Persistent.ObjectTable ot = new MessageManager().GetMessageList(UserID);
                String key = Keys.SecurityKey[SessionHeader.Version];
                foreach (var row in ot)
                {
                    MessageView view = (MessageView)row;
                    Message message = new Message();
                    message.Title = view.TOPIC;
                    message.ID = view.ID.ToString();
                    message.Time = view.CREATETIME.ToString();
                    message.ReadFlag = view.READFLAG;
                    if (!String.IsNullOrEmpty(view.PHOTOID))
                        message.ImageURL = path + "IMAGE.ashx?" + view.PHOTOID;
                    String NewRequestKey = Jumpton.Common.Functions.EncryptString(view.RECEIVEID + key);
                    message.URL = path + String.Format("MESSAGE.aspx?Version={0}&ID={1}&RequestKey={2}", SessionHeader.Version, view.RECEIVEID, NewRequestKey);
                    list.Add(message);
                }
                result.Successful = true;
                result.Values = list;
            }
            catch(Exception err)
            {
                result.Message = err.Message;
            }

        }

        var js = new System.Web.Script.Serialization.JavaScriptSerializer();
        return js.Serialize(result);
    }

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Test(String UserID)
    {

        return ValidRequest(new String[] { "62", "2012.6.20.14.51.38"}, "0.01", "DA94A0E9BCE9B0573FE157CB62E84DA7").ToString();
    }

    private Boolean ValidRequest(String[] values, String Version, String RequestKey)
    {
        String key = Keys.SecurityKey[Version];
        String str = "";
        foreach (String s in values)
        {
            str += s;
        }
        str += key;
        return Jumpton.Common.Functions.EncryptString(str) == RequestKey;
      
    }
    
}
